package com.kaluli.project.utils.nanshaxml;
import java.sql.Timestamp;
/**
 * 此类用于组装返利网Xml的推送报文
 * 此类的工作环境是多线程
 * @author zyzowen1944
 *  Date 2016-2-2
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.http.client.utils.DateUtils;
import org.dom4j.*;
import org.dom4j.io.*;
public  class EDIXmlBuilder {
	private static EDIXmlBuilder xmlBuilder=null;
	private static Document document = null;
	private final static String ROOT="request";
	private final static String HEAD = "head";
	
	public final static String METHOD_TYPE = "methodType";
	public final static String HG_CODE = "hgCode";
	public final static String BUS_CODE = "busCode";
	
	private final static String BODY = "body";
	public final static String EDI_BODY = "order";
	public final static String ORDERSHOP = "orderShop";
	public final static String OTOCODE = "otoCode";//OTO店铺代码
	public final static String HGAREA = "hgArea";//海关关区
	public final static String ORDERFROM = "orderFrom";
	public final static String PACKAGEFLAG = "packageFlag";
	public final static String BUSORDERNO = "busOrderNo";
	public final static String POSTFEE = "postFee";
	public final static String INSURANCEFEE = "insuranceFee";
	public final static String AMOUNT = "amount";
	public final static String BUYERACCOUNT = "buyerAccount";
	public final static String PHONE = "phone";
	public final static String EMAIL = "email";
	public final static String TAXAMOUNT = "taxAmount";
	public final static String TARIFFAMOUNT = "tariffAmount";
	public final static String ADDEDVALUETAXAMOUNT = "addedValueTaxAmount";
	public final static String CONSUMPTIONDUTYAMOUNT = "consumptionDutyAmount";
	public final static String GROSSWEIGHT = "grossWeight";
	public final static String DISAMOUNT = "disAmount";
	public final static String DEALDATE = "dealDate";
	public final static String REMARK = "remark";
	
	public final static String PROMOTIONS = "promotions";//
	public final static String PROMOTION = "promotion";//
	public final static String PROAMOUNT = "proAmount";
	public final static String PROREMARK = "proRemark";
	
	public final static String GOODS = "goods";//
	public final static String DETAIL = "detail";//
	public final static String PRODUCTID = "productId";
	public final static String QTY = "qty";
	public final static String PRICE = "price";
	public final static String G_AMOUNT = "amount";
	
	public final static String ISPAYPUSH = "isPayPush";
	public final static String PAY = "pay";//
	public final static String PAYTIME = "paytime";
	public final static String PAYMENTNO = "paymentNo";
	public final static String ORDERSEQNO = "orderSeqNo";
	public final static String SOURCE = "source";
	public final static String IDNUM = "idnum";
	public final static String NAME = "name";
	public final static String MERID = "merId";
	
	public final static String LOGISTICS = "logistics";
	public final static String LOGISTICSNO =  "logisticsNo";
	public final static String LOGISTICSNAME = "logisticsName";
	public final static String LOGISTICSCODE = "logisticsCode";
	public final static String CONSIGNEE = "consignee";
	public final static String PROVINCE = "province";
	public final static String CITY = "city";
	public final static String DISTRICT = "district";
	public final static String CONSIGNEEADDR="consigneeAddr";
	public final static String CONSIGNEETEL = "consigneeTel";
	public final static String MAILNO = "mailNo";
	public final static String GOODSNAME = "goodsName";
	
	
	
	
	
	public  static EDIXmlBuilder getInstance(){
		synchronized(EDIXmlBuilder.class){  //避免多线程调用该方法时串行执行，效率低下
			//if(xmlBuilder==null){
				xmlBuilder =  new EDIXmlBuilder();
				
			//}
		}
		return xmlBuilder;
	}
	
	/**
	 * 创建海关Xml组装器
	 * @param root
	 * @param head
	 * @param body
	 * @return
	 */
	public static EDIXmlBuilder getInstance(String type){
		synchronized(EDIXmlBuilder.class){  //避免多线程调用该方法时串行执行，效率低下
			xmlBuilder = new EDIXmlBuilder(type);
		}
		return xmlBuilder;
	}
	
	
	public EDIXmlBuilder(){
		document = DocumentFactory.getInstance().createDocument();
		document.addElement(ROOT);
		Element root = document.getRootElement();
		root.addElement(HEAD);
		root.addElement(BODY);
	}
	
	public EDIXmlBuilder(String root){
		document = DocumentFactory.getInstance().createDocument();
		document.addElement(EDI_BODY);
		Element roots = document.getRootElement();
//		roots.addElement(HEAD);
		roots.addElement(EDI_BODY);
	}
	
	public void addHeader(String key,String value){
		String xpath = "//"+HEAD;
		@SuppressWarnings("unchecked")
		List<Element> composites = document.selectNodes(xpath);
		Element head = composites.get(0);
		head.addElement(key).addText(value);
	}
	
	public void addBody(String key,Object value,boolean isleaf,String parentName){
		String xpath = "/"+parentName;
		@SuppressWarnings("unchecked")
		List<Element> composites = document.selectNodes(xpath);
		Element body = composites.get(composites.size()-1);
		if(isleaf){ //如果是叶子节点，同时创建Tag和Value
			if(value.getClass().getName().equals(String.class.getName())){
				body.addElement(key).addText(value.toString());
			}else if(value.getClass().getName().equals(Date.class.getName()) || value.getClass().getName().equals(Timestamp.class.getName())){
				String date = DateUtils.formatDate((Date) value, "yyyyMMddHHmmss");
				body.addElement(key).addText(date);
			}else{
				body.addElement(key).addText(value.toString());
			}
			
		}else{  //如果是子节点
			body.addElement(key);
		}
	}
	
	public String toString(){
		return document.asXML();
	}
	public static void main(String s[]){
		/*EDIXmlBuilder xml = EDIXmlBuilder.getInstance("HG");
		xml.addHeader("MessageID", "CUST000000012312313");
		xml.addBody("swbebtrade", "", false, ROOT+"/"+BODY);
		xml.addBody("Record","",false,ROOT+"/"+BODY+"/swbebtrade");
		xml.addBody("EntInsideNo", "test0001", true, ROOT+"/"+BODY+"/swbebtrade/Record");//增加叶子 
		xml.addBody("swbetradeg",null, false	, ROOT+"/"+BODY+"/swbebtrade/Record");  //增加节点 
		xml.addBody("Record", "", false	, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg");  //增加节点 
		xml.addBody("EntGoodsNo", "1", true, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg/Record");
		xml.addBody("Record", "", false	, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg");  //增加节点 
		xml.addBody("EntGoodsNo", "2", true, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg/Record");
		//xml.addBody("", value, isleaf, parentName)
		
		//xml.addBody("EntGoodsNo", "1", true, parentName)
		System.out.println(xml.toString());*/
		SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
		String s2 = sFormat.format(new Date());
		System.out.print(s2);
	}
	
}
